package com.fjrcloud.aop;

import com.fjrcloud.utils.ConfigInfo;
import com.fjrcloud.utils.EncryptUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

/**
 * Author: Trun
 * Date: 2016-11-24
 * Description: HtmlIntercept.java
 */

@Aspect
@Component
@Order(1)
public class CorrectIntercept {

    @Autowired
    private ConfigInfo configInfo;
    @Autowired
    private HttpServletRequest request;

    public static String hexByte(byte b) {
        String s = "000000" + Integer.toHexString(b);
        return s.substring(s.length() - 2);
    }

    private String getMAC() {
        Enumeration<NetworkInterface> el;
        String mac_s = "";
        try {
            el = NetworkInterface.getNetworkInterfaces();
            while (el.hasMoreElements()) {
                byte[] mac = el.nextElement().getHardwareAddress();
                if (mac == null)
                    continue;
                mac_s = hexByte(mac[0]) + "-" + hexByte(mac[1]) + "-"
                        + hexByte(mac[2]) + "-" + hexByte(mac[3]) + "-"
                        + hexByte(mac[4]) + "-" + hexByte(mac[5]);
            }
        } catch (SocketException e1) {
            e1.printStackTrace();
        }
        return mac_s.toUpperCase();
    }

    private void check() throws Exception {
        String auth = configInfo.getAuthorization();
        if (StringUtils.isEmpty(auth))
            throw new Exception("没有授权");

        auth = EncryptUtil.decrypt(auth);

        Map<String, String> map = new Gson().fromJson(auth,new TypeToken<Map<String, String>>() {}.getType());

        String mac = map.get("mac");
        if (StringUtils.isEmpty(mac))
            throw new Exception("授权错误");

        String localMac = getMAC();
        if (StringUtils.isEmpty(localMac))
            throw new Exception("无法确定服务器MAC");

        if (!mac.equals(localMac))
            throw new Exception("授权服务器");

        String dataString = map.get("overdue");
        if (StringUtils.isEmpty(dataString))
            throw new Exception("授权过期");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dataString);

        if (date.getTime() - System.currentTimeMillis() < 0)
            throw new Exception("授权过期");
    }

//    @Around("execution(public * com.fjrcloud.service..*.*(..))")
    public Object login(ProceedingJoinPoint point) throws Throwable {

        Boolean correct = (Boolean) request.getSession().getAttribute("correct");

        if (correct != null && correct){
            return point.proceed();
        }else{

            try {
                check();
                request.getSession().setAttribute("correct",true);
                return point.proceed();
            }catch (Exception e){
                System.out.println(e.getMessage());
                request.getSession().setAttribute("correct",false);
            }
        }
        return null;
    }

}
